package com.community.controller;

/**
 * 文件上传控制层
 *
 * @author xn
 * @since 2021-09-27 16:45:30
 */

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.community.util.FIleUtils;

import java.io.*;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("upload")
public class UploadController extends ApiController {
    @Autowired
    private FIleUtils fIleUtils;

    @Value("${file.uploadFolder}")
    private String uploadFolder;

    /**
     * 新增图片
     */
    @PostMapping("uploadFile")
    @ResponseBody
    public Object uploadFile(@RequestParam(value = "images", required = false) MultipartFile fileUpload) {
//        String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "upload");
        String fileName = fileUpload.getOriginalFilename();
        return success(this.fIleUtils.upload(uploadFolder, fileName, fileUpload));
    }

    /**
     * 删除图片
     */
    @PostMapping("deleteFile")
    @ResponseBody
    public Object uploadFile(@RequestParam(value = "filePath", required = false) String filePath) {
        return success(this.fIleUtils.deleteFile(filePath));
    }

    /**
     * 上传Excel文件
     */
    @PostMapping("excel")
    @ResponseBody
    public Object uploadExcel(@RequestParam(value = "addExcel", required = false) MultipartFile multipartFile) {
        //String fileName = multipartFile.getOriginalFilename();
        String file = "D://T296晨练罚款班级.xls";
        //处理乱码
       /* WorkbookSettings workbookSettings = new WorkbookSettings();
        workbookSettings.setEncoding("ISO-8859-1");*/
        //获取Excel文件对象
        Workbook rwb = null;
        try {
            rwb = Workbook.getWorkbook(new File(file));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        //获取文件的指定工作表 默认的第一个
        Sheet sheet = rwb.getSheet(0);
        List list = new ArrayList();
        //行数(表头的目录不需要，从1开始)
        for (int i = 0; i < sheet.getRows(); i++) {
            //创建一个数组 用来存储每一列的值
            String[] str = new String[sheet.getColumns()];
            Cell cell = null;
            //列数
            for (int j = 0; j < sheet.getColumns(); j++) {
                //获取第i行，第j列的值
                cell = sheet.getCell(j, i);
                str[j] = cell.getContents();
            }
            //把刚获取的列存入list
            list.add(str);
        }
        if (list != null) {
            for (Object o : list) {
                String[] s = (String[]) o;
                for (int i = 0; i < s.length; i++) {
                    System.out.println(s[i]);
                }
            }
        }
        return null;
        //return success(this.fIleUtils.upload(uploadFolder, fileName, multipartFile));
    }
}
